/*
 * =====================================================================================
 *
 *       Filename:  Config.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年04月09日 14时34分55秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  LiuYi (), swliuyi@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include "Config.h"
Config::Config(string configPath)
{
    fstream file;
    file.open(configPath.c_str());
    if( !file.is_open() )
    {
        cout<<"load config "<<configPath<<" error."<<endl;
        return;
    }
    string line;
    string root;
    while( getline(file, line) )
    {
        if( line[0] == '#' || line[0] == '\n' )
        {
            continue; 
        }
        else if(line.substr(0, 5) == "root=")
        {
            root = line.substr(5, line.size()-5);
            cout<<root<<endl;
        }
        else
        {
            vector<string> d;
            size_t pos = -3;
            for( int i = 0 ; i < 4 ; i++ )
            {
                line = line.substr(pos+3, line.size()-pos-3);
                pos = line.find("|||");
                if( pos == line.npos )
                {
                    cout<<"load config item "<<line<<" error."<<endl;
                    continue;
                }
                d.push_back(line.substr(0, pos));
            }
            line = line.substr(pos+3, line.size()-pos-3);
            d.push_back(line);
            ConfigItem ci(d[0], d[1], root+d[2], root+d[3], atoi(d[4].c_str()));
            m_data.push_back(ci);
        }
    }
    for( int i = 0 ; i < m_data.size() ; i++ )
    {
        cout<<m_data[i].GetLang()<<endl;
        cout<<m_data[i].GetCode()<<endl;
        cout<<m_data[i].GetCharPath()<<endl;
        cout<<m_data[i].GetLMPath()<<endl;
        cout<<m_data[i].GetGramNum()<<endl;
    }
}

